// Copyright 2011 Google Inc. All Rights Reserved.
// Author: jacobsa@google.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var GraphEdge = flame.internal.GraphEdge;
var findCycle = flame.internal.findCycle;

//////////////////////////////////////////////////////
// Helpers
//////////////////////////////////////////////////////

function FindCycleTest() {}
registerTestSuite(FindCycleTest);

//////////////////////////////////////////////////////
// Tests
//////////////////////////////////////////////////////

FindCycleTest.prototype.EmptyGraph = function() {
  var edges = [
  ];

  var result = findCycle(edges);
  expectThat(result, isNull);
};

FindCycleTest.prototype.OneNodeWithSelfCycle = function() {
  var edges = [
      new GraphEdge('taco', 'taco'),
  ];

  var result = findCycle(edges);
  expectThat(result, equals('taco'));
};

FindCycleTest.prototype.TwoNodesWithoutCycle = function() {
  var edges = [
      new GraphEdge('taco', 'burrito'),
  ];

  var result = findCycle(edges);
  expectThat(result, isNull);
};

FindCycleTest.prototype.TwoNodesWithCycle = function() {
  var edges = [
      new GraphEdge('taco', 'burrito'),
      new GraphEdge('burrito', 'taco'),
  ];

  var result = findCycle(edges);
  expectThat(result, anyOf(['burrito', 'taco']));
};

FindCycleTest.prototype.ManyNodesInTree = function() {
  var edges = [
      new GraphEdge('taco', 'burrito'),
          new GraphEdge('burrito', 'enchilada'),
          new GraphEdge('burrito', 'queso'),
              new GraphEdge('queso', 'nachos'),
          new GraphEdge('burrito', 'carnitas'),
      new GraphEdge('taco', 'corn chips'),
          new GraphEdge('corn chips', 'tortilla'),
  ];

  var result = findCycle(edges);
  expectThat(result, isNull);
};

FindCycleTest.prototype.ManyNodesInNonTreeWithoutCycle = function() {
  var edges = [
      new GraphEdge('taco', 'burrito'),
      new GraphEdge('burrito', 'enchilada'),
      new GraphEdge('burrito', 'corn chips'),
      new GraphEdge('enchilada', 'queso'),
      new GraphEdge('taco', 'queso'),
      new GraphEdge('queso', 'carnitas'),
      new GraphEdge('queso', 'tortilla'),
      new GraphEdge('carnitas', 'corn chips'),
      new GraphEdge('tortilla', 'corn chips'),
  ];

  var result = findCycle(edges);
  expectThat(result, isNull);
};

FindCycleTest.prototype.ManyNodesWithSelfCycle = function() {
  var edges = [
      new GraphEdge('taco', 'burrito'),
      new GraphEdge('burrito', 'enchilada'),
      new GraphEdge('burrito', 'corn chips'),
      new GraphEdge('enchilada', 'queso'),
      new GraphEdge('taco', 'queso'),
      new GraphEdge('queso', 'carnitas'),
      new GraphEdge('queso', 'tortilla'),
      new GraphEdge('queso', 'queso'),  // Self-cycle
      new GraphEdge('carnitas', 'corn chips'),
      new GraphEdge('tortilla', 'corn chips'),
  ];

  var result = findCycle(edges);
  expectThat(result, equals('queso'));
};

FindCycleTest.prototype.ManyNodesWithNonSelfCycle = function() {
  var edges = [
      new GraphEdge('taco', 'burrito'),
      new GraphEdge('burrito', 'enchilada'),
      new GraphEdge('burrito', 'corn chips'),
      new GraphEdge('enchilada', 'queso'),
      new GraphEdge('taco', 'queso'),
      new GraphEdge('queso', 'carnitas'),
      new GraphEdge('queso', 'tortilla'),
      new GraphEdge('carnitas', 'corn chips'),
      new GraphEdge('tortilla', 'corn chips'),
      new GraphEdge('corn chips', 'queso'),
      new GraphEdge('corn chips', 'tamale'),
  ];

  var result = findCycle(edges);
  expectThat(result, anyOf(['queso', 'carnitas', 'tortilla', 'corn chips']));
};

FindCycleTest.prototype.DisconnectedComponentsWithoutCycle = function() {
  var edges = [
      new GraphEdge('taco', 'burrito'),
      new GraphEdge('burrito', 'enchilada'),
      new GraphEdge('burrito', 'corn chips'),
      new GraphEdge('enchilada', 'corn chips'),

      new GraphEdge('queso', 'carnitas'),
      new GraphEdge('queso', 'tortilla'),
      new GraphEdge('carnitas', 'corn chips'),
  ];

  var result = findCycle(edges);
  expectThat(result, isNull);
};

FindCycleTest.prototype.DisconnectedComponentsWithCycle = function() {
  var edges = [
      new GraphEdge('taco', 'burrito'),
      new GraphEdge('burrito', 'enchilada'),
      new GraphEdge('burrito', 'corn chips'),
      new GraphEdge('enchilada', 'corn chips'),

      new GraphEdge('queso', 'carnitas'),
      new GraphEdge('queso', 'tortilla'),
      new GraphEdge('carnitas', 'corn chips'),
      new GraphEdge('corn chips', 'queso'),
  ];

  var result = findCycle(edges);
  expectThat(result, anyOf(['queso', 'carnitas', 'corn chips']));
};

FindCycleTest.prototype.NodesWithSpecialNamesInCycle = function() {
  var edges = [
      new GraphEdge('prototype', 'constructor'),
      new GraphEdge('constructor', 'hasOwnProperty'),
      new GraphEdge('hasOwnProperty', 'prototype'),
  ];

  var result = findCycle(edges);
  expectThat(result, anyOf(['prototype', 'constructor', 'hasOwnProperty']));
};
